package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper baseMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private RoomPaymentTypeMapper paymentTypeMapper;
    @Autowired
    private RoomLeaseTermMapper leaseTermMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //保存或更新房间信息

    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        //获取房间的id 用于后续的处理
        boolean isUpdate= roomSubmitVo.getId() != null;
        //保存房间信息
        super.saveOrUpdate(roomSubmitVo);
        //假如获取到了id证明是更新的操作 然后我们将房间图片，房间标签，房间配套等先删除 后在保存
        if(isUpdate){
            //删除房间图片
            LambdaQueryWrapper<GraphInfo> graphqueryWrapper = new LambdaQueryWrapper<>();
            graphqueryWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
            graphqueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphqueryWrapper);
            //删除属性信息列表
            LambdaQueryWrapper<RoomAttrValue> roomqueryWrapper = new LambdaQueryWrapper<>();
            roomqueryWrapper.eq(RoomAttrValue::getRoomId,roomSubmitVo.getId());
            roomAttrValueService.remove(roomqueryWrapper);
            //删除配套设施列表
            LambdaQueryWrapper<RoomFacility> facilityqueryWrapper = new LambdaQueryWrapper<>();
            facilityqueryWrapper.eq(RoomFacility::getRoomId,roomSubmitVo.getId());
            roomFacilityService.remove(facilityqueryWrapper);
            //删除标签列表
            LambdaQueryWrapper<RoomLabel> labelqueryWrapper = new LambdaQueryWrapper<>();
            labelqueryWrapper.eq(RoomLabel::getRoomId,roomSubmitVo.getId());
            roomLabelService.remove(labelqueryWrapper);
            //删除支付方式列表
            LambdaQueryWrapper<RoomPaymentType> paymentqueryWrapper = new LambdaQueryWrapper<>();
            paymentqueryWrapper.eq(RoomPaymentType::getRoomId,roomSubmitVo.getId());
            roomPaymentTypeService.remove(paymentqueryWrapper);
            //删除租约列表
            LambdaQueryWrapper<RoomLeaseTerm> leasequeryWrapper = new LambdaQueryWrapper<>();
            leasequeryWrapper.eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId());
            roomLeaseTermService.remove(leasequeryWrapper);

            //删除缓存
            redisTemplate.delete(RedisConstant.APP_LOGIN_PREFIX + roomSubmitVo.getId());
        }
        //插入图片列表
        //获取图片列表 本质是对GraphInfo表的操作
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //插入属性信息列表
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        //插入配套信息列表
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<RoomFacility> facilityList = new ArrayList<>();
            for (Long facilityId : facilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder().build();
                roomFacility.setRoomId(roomSubmitVo.getId());
                roomFacility.setFacilityId(facilityId);
                facilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(facilityList);
        }
        //插入标签列表
        List<Long> labelIds = roomSubmitVo.getLabelInfoIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            ArrayList<RoomLabel> labelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                RoomLabel roomLabel = RoomLabel.builder().build();
                roomLabel.setRoomId(roomSubmitVo.getId());
                roomLabel.setLabelId(labelId);
                labelList.add(roomLabel);
            }
            roomLabelService.saveBatch(labelList);
        }
        //插入支付方式列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            ArrayList<RoomPaymentType> paymentList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().build();
                roomPaymentType.setRoomId(roomSubmitVo.getId());
                roomPaymentType.setPaymentTypeId(paymentTypeId);
                paymentList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(paymentList);
        }
        //插入租约列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            ArrayList<RoomLeaseTerm> leaseList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().build();
                roomLeaseTerm.setRoomId(roomSubmitVo.getId());
                roomLeaseTerm.setLeaseTermId(leaseTermId);
                leaseList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(leaseList);
        }
    }

    //分页查询

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page,queryVo);
    }


    //根据id查询房间信息

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        //1.查询RoomInfo
        RoomInfo roomInfo = roomInfoMapper.selectById(id);

        //2.查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //3.查询graphInfoList
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);

        //4.查询attrValueList
        List<AttrValueVo> attrvalueVoList = attrValueMapper.selectListByRoomId(id);

        //5.查询facilityInfoList
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);

        //6.查询labelInfoList
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);

        //7.查询paymentTypeList
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);

        //8.查询leaseTermList
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);


        RoomDetailVo adminRoomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, adminRoomDetailVo);

        adminRoomDetailVo.setApartmentInfo(apartmentInfo);
        adminRoomDetailVo.setGraphVoList(graphVoList);
        adminRoomDetailVo.setAttrValueVoList(attrvalueVoList);
        adminRoomDetailVo.setFacilityInfoList(facilityInfoList);
        adminRoomDetailVo.setLabelInfoList(labelInfoList);
        adminRoomDetailVo.setPaymentTypeList(paymentTypeList);
        adminRoomDetailVo.setLeaseTermList(leaseTermList);

        return adminRoomDetailVo;
    }



    //根据id删除房间信息

    @Override
    public void roomInforemoveById(Long id) {
        //先将房间信息表中的数据删除
        super.removeById(id);
        //之后再删除其他关联表数据
        //删除房间图片
        LambdaQueryWrapper<GraphInfo> graphqueryWrapper = new LambdaQueryWrapper<>();
        graphqueryWrapper.eq(GraphInfo::getItemId,id);
        graphqueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(graphqueryWrapper);
        //删除属性信息列表
        LambdaQueryWrapper<RoomAttrValue> roomqueryWrapper = new LambdaQueryWrapper<>();
        roomqueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomqueryWrapper);
        //删除配套设施列表
        LambdaQueryWrapper<RoomFacility> facilityqueryWrapper = new LambdaQueryWrapper<>();
        facilityqueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(facilityqueryWrapper);
        //删除标签列表
        LambdaQueryWrapper<RoomLabel> labelqueryWrapper = new LambdaQueryWrapper<>();
        labelqueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(labelqueryWrapper);
        //删除支付方式列表
        LambdaQueryWrapper<RoomPaymentType> paymentqueryWrapper = new LambdaQueryWrapper<>();
        paymentqueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(paymentqueryWrapper);
        //删除租约列表
        LambdaQueryWrapper<RoomLeaseTerm> leasequeryWrapper = new LambdaQueryWrapper<>();
        leasequeryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(leasequeryWrapper);

        //8.删除缓存
        redisTemplate.delete(RedisConstant.APP_ROOM_PREFIX + id);

    }
    }






